package com.resou.reader.bookdetail.comment.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.resou.reader.R;
import com.resou.reader.api.entry.BaseComment;
import com.resou.reader.api.entry.CommentBean;
import com.resou.reader.base.ResouBaseFragment;
import com.resou.reader.bookdetail.adapter.CommentListAdapter;
import com.resou.reader.bookdetail.comment.detail.CommentDetailActivity;
import com.resou.reader.data.local.LitePalHelper;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.utils.CircularAnimUtil;
import com.resou.reader.utils.PreferenceUtil;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.eventbus.MessageEvent;
import com.resou.reader.view.Loading;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description: 最新评论
 * CreateTime: 2019-04 -10
 **/
public class FragmentHot extends ResouBaseFragment<FragmentCommentPresenter> implements IFragmentCommentContract.IFragmentCommentView, OnRefreshLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.llContent)
    LinearLayout llContent;
    @BindView(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    CommentListAdapter commentListAdapter;
    int page = 0;
    String mBookId;
    BaseComment.DataBean bean;
    String type = "2";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_bookcomment;
    }

    public FragmentHot() {
    }


    @Override
    protected void initPresenter() {
        mPresenter = new FragmentCommentPresenter(this);
    }


    @Override
    protected void initView() {
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        mBookId = getArguments().getString("mBookId");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        commentListAdapter = new CommentListAdapter(R.layout.commend_item_layout, null);
        mRecyclerView.setAdapter(commentListAdapter);
        mSmartRefreshLayout.setOnRefreshLoadMoreListener(this);
        commentListAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void retryRefreshData() {
        page = 0;
        mPresenter.quearComment(mBookId, type, page);
    }

    @Override
    public void initData() {
//        page = 0;
//        mPresenter.quearComment(mBookId, type, page);
        mSmartRefreshLayout.autoRefresh();
    }

    @Override
    public void showContent() {
//        dissLoading();
    }

    @Override
    public void showProgress() {
//        showLoading();
    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        if (page == 0) {
            showNetErrorView(R.id.rlContent);
        }
    }

    @Override
    public void showEmpty() {
        commentListAdapter.setEmptyView(LayoutInflater.from(getActivity()).inflate(R.layout.commentlistempty, null));
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        mPresenter.quearComment(mBookId, type, page);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 0;
        mPresenter.quearComment(mBookId, type, page);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        bean = (BaseComment.DataBean) adapter.getData().get(position);
        switch (view.getId()) {
            case R.id.llCommentOnclickLister:
                CommentDetailActivity.start(getContext(), bean.getNovel_id() + "", bean.getId() + "");
                break;
            case R.id.llPraise:
                if (PreferenceUtil.getInstance(getContext()).getlocalCommentPariseState(bean.getId() + "")) {
//                    ToastUtil.makeShortToast("已经点赞");
                    CircularAnimUtil.propertyValuesHolderDown(view);
                } else {
                    mPresenter.remoteLikeParise(1, bean.getId());
                }
                break;
            case R.id.llComment:
                break;
        }
    }


    @Override
    public void showComment(List<BaseComment.DataBean> list, String totalComment) {
        if (totalComment.equals("0")) {
            showEmpty();
        }
        if (page == 0) {
            commentListAdapter.setNewData(list);
        } else {
            commentListAdapter.addData(list);
        }

    }


    @Subscribe(threadMode = ThreadMode.POSTING)
    public void Event(CommentBean commentBean) {
        page = 0;
        mPresenter.quearComment(mBookId, type, page);
    }

    @Override
    public SmartRefreshLayout getSmartRefreshLayout() {
        return mSmartRefreshLayout;
    }

    @Override
    public void likePariseSuccess() {
        PreferenceUtil.getInstance(getContext()).setlocalCommentPariseState(bean.getId() + "");
        page = 0;
//        mPresenter.quearComment(mBookId, type, page);
        EventBus.getDefault().post(new CommentBean());

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
